<script>
const doWidthListener = function (listeners, on) {
  let out = Object.assign({}, listeners)
  _.forOwn(on, (value, key) => {
    if (out[key]) {
      out[key] = [out[key], value]
    } else {
      out[key] = value
    }
  })
  return out
}

const TypeOption = {
  'text': function (h) {
    return h('div', [this.value])
  },
  'input': function (h) {
    let props = Object.assign({ value: this.value, clearable: false, }, this.$attrs, this.attrs)
    let { placeholder, maxlength, minlength, ...new_props } = props


    if (placeholder != undefined && placeholder != null) {
      placeholder = this.$tt(placeholder)
    } else {
      placeholder = this.$tt('tip_please_input')
    }
    let on = doWidthListener(this.$listeners, this.on)
    let child = []
    if (this.$slots.prepend || props.prepend) child.push(h('div', {
      slot: 'prepend',
      // style: props.prependStyle,
      // class: props.prependClass
    }, this.$slots.prepend || this.$tt(props.prepend)))
    if (this.$slots.append || props.append) child.push(h('div', {
      slot: 'append',
      // style: props.appendStyle,
      // class: props.appendClass
    }, this.$slots.append || this.$tt(props.append)))
    return h('el-input', { attrs: { placeholder, maxlength, minlength }, props: new_props, on, ref: 'main', }, child)
  },
  'select': function (h) {

    let props = Object.assign({ value: this.value, clearable: false, }, this.$attrs, this.attrs)
    props.placeholder = this.$tt(props.placeholder || 'tip_please_input')
    let pickKeys = ['i18n', 'storeData', 'option', 'url', 'urlType', 'urlParams', 'urlControl',
      'enum', 'listHandler', 'items', 'cache', 'value', 'attrs']
    let new_props = _.pick(props, pickKeys)
    let new_attrs = _.omit(props, pickKeys)
    // const componentType = Vue.component('ex_select_auto')
    // const componentTypeProps = componentType.props || componentType
    // const componentTypeProps = select_auto.props || select_auto
    // console.log(componentType, 'ex_select_auto')
    let on = doWidthListener(this.$listeners, this.on)
    return h('select_auto', { props: new_props, attrs: new_attrs, on, ref: 'main', })
  },
  'radio-group': function (h) {

    let props = Object.assign({ value: this.value, }, this.$attrs, this.attrs)

    let pickKeys = ['i18n', 'storeData', 'option', 'url', 'urlType', 'urlParams', 'urlControl',
      'enum', 'listHandler', 'items', 'cache', 'value', 'attrs', 'border']

    let new_props = _.pick(props, pickKeys)
    let new_attrs = _.omit(props, pickKeys)
    let on = doWidthListener(this.$listeners, this.on)
    return h('radio_group_auto', { props: new_props, attrs: new_attrs, on, })
  },
  'checkbox-group': function (h) {

    let props = Object.assign({ value: this.value, }, this.$attrs, this.attrs)

    let pickKeys = ['i18n', 'storeData', 'option', 'url', 'urlType', 'urlParams', 'urlControl',
      'enum', 'listHandler', 'items', 'cache', 'value', 'attrs', 'border']

    let new_props = _.pick(props, pickKeys)
    let new_attrs = _.omit(props, pickKeys)
    let on = doWidthListener(this.$listeners, this.on)
    return h('checkbox_group_auto', { props: new_props, attrs: new_attrs, on, })
  },
  'cascader': function (h) {
    let props = Object.assign({ value: this.value, }, this.$attrs, this.attrs)

    return h('el-cascader', { props, on: this.$listeners, ref: 'main', })
  },
  'time-select': function (h) {
    let props = Object.assign({ value: this.value, clearable: false,'value-format': "timestamp" }, this.$attrs, this.attrs)
    return h('el-time-select', { props, on: this.$listeners, ref: 'main', })
  },
  'time-picker': function (h) {
    let props = Object.assign({ value: this.value, clearable: false,'value-format': "timestamp" }, this.$attrs, this.attrs)
    return h('el-time-picker', { props, on: this.$listeners, ref: 'main', })
  },
  'date-picker': function (h) {
    let props = Object.assign({ value: this.value, clearable: false,'value-format': "timestamp" }, this.$attrs, this.attrs)
    if (props.placeholder) props.placeholder = this.$tt(props.placeholder)
    //用于生成固定标记时间选择
    // let op = props.pickerOptions || props["picker-options"]
    // if (op && op.shortcuts) {
    //     if (op.shortcuts == "defaultAfter") {

    //         op.shortcuts = [
    //             {
    //                 text: '明天',
    //                 onClick(picker) {
    //                     const date = new Date();
    //                     date.setTime(date.getTime() + 3600 * 1000 * 24);
    //                     picker.$emit('pick', date);
    //                 }
    //             },
    //             {
    //                 text: '后天',
    //                 onClick(picker) {
    //                     const date = new Date();
    //                     date.setTime(date.getTime() + 3600 * 1000 * 24 * 2);
    //                     picker.$emit('pick', date);
    //                 }
    //             },
    //             {
    //                 text: '大后天',
    //                 onClick(picker) {
    //                     const date = new Date();
    //                     date.setTime(date.getTime() + 3600 * 1000 * 24 * 3);
    //                     picker.$emit('pick', date);
    //                 }
    //             },
    //             {
    //                 text: '一周后',
    //                 onClick(picker) {
    //                     const date = new Date();
    //                     date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
    //                     picker.$emit('pick', date);
    //                 }
    //             },
    //             {
    //                 text: '两周后',
    //                 onClick(picker) {
    //                     const date = new Date();
    //                     date.setTime(date.getTime() + 3600 * 1000 * 24 * 14);
    //                     picker.$emit('pick', date);
    //                 }
    //             },
    //             {
    //                 text: '三周后',
    //                 onClick(picker) {
    //                     const date = new Date();
    //                     date.setTime(date.getTime() + 3600 * 1000 * 24 * 21);
    //                     picker.$emit('pick', date);
    //                 }
    //             },
    //         ]
    //     }
    // }
    return h('el-date-picker', { props, on: this.$listeners })
  },
  'input-number': function (h) {
    let props = Object.assign({ value: this.value, }, this.$attrs, this.attrs)
    let on = doWidthListener(this.$listeners, this.on)
    return h('el-input-number', { props, on, ref: 'main', })
  },
  'upload-img': function (h) {
    let props = Object.assign({ value: this.value, }, this.$attrs, this.attrs)
    return h('ex-upload-img', { props, on: this.$listeners, ref: 'main', })
  },
  'upload-file': function (h) {
    let props = Object.assign({ value: this.value, }, this.$attrs, this.attrs)
    return h('ex-upload-file', { props, on: this.$listeners, ref: 'main', })
  },

  // 'ums-customer-choose': function (h) {

  //   let props = Object.assign({ value: this.value, }, this.$attrs, this.attrs)
  //   console.log("123321",props)
  //   return h('ums-customer-choose', { props, on: this.$listeners, ref: 'main', })
  // },
}
const other_h = function (h) {
  const props_attrs = Object.assign({ value: this.value, }, this.$attrs, this.attrs)
  const on = doWidthListener(this.$listeners, this.on)

  const componentType = Vue.component(this.type)
  if (componentType) {
    const componentTypeProps = componentType.sealedOptions.props
    const props = _.pickBy(props_attrs, (value, key) => {
      return componentTypeProps[key] != undefined
    });
    const attrs = _.pickBy(props_attrs, (value, key) => {
      return componentTypeProps[key] == undefined
    });

    return h(componentType, { props, attrs, on, ref: 'main', })
  } else {
    return h(this.type, { props: props_attrs, on, ref: 'main', })

  }

}

import Vue from 'vue'
import select_auto from './select_auto.vue'
import checkbox_group_auto from './checkbox_group_auto.vue'
import radio_group_auto from './radio_group_auto.vue'



export default {
  name: 'super-input',
  components: {
    select_auto, checkbox_group_auto, radio_group_auto,
  },
  render: function (h) {
    if(this.type instanceof Object){
      return other_h.call(this, h)
    }
    let opt = TypeOption[this.type || 'input']
    if (!opt) return other_h.call(this, h)
    return opt.call(this, h)
  },
  props: {
    value: null,
    type: {
      type:[String,Object],
      default:"",
    },
    attrs: Object,

    on: Object,
  },

  methods: {
    getMain() {
      return this.$refs['main']
    },

  }
}
</script>
<style></style>
